package com.codeoss.module.sale.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.module.ir.service.IIrReportQueryService;
import com.codeoss.module.sale.service.ISaleStatisticsReportService;

@Service
public class SaleStatisticsReportService implements ISaleStatisticsReportService {
	@Autowired
	private IIrReportQueryService irReportQueryService;
	@Override
	public List<Map<String,Object>> loadData(String dsName,String datasetName,Map<String,Object> parameters){
		List<Map<String,Object>> list=irReportQueryService.search("sale_statistics_report", parameters);
		for(int i=0;i<list.size();i++) {
			Map<String,Object> dataMap=list.get(i);
			BigDecimal qtyOrdered=(BigDecimal)dataMap.get("qty_ordered");
			if(qtyOrdered==null) {
				qtyOrdered=new BigDecimal(0);
				dataMap.put("qty_ordered", qtyOrdered);
			}
			BigDecimal amountSubtotal=(BigDecimal)dataMap.get("amount_subtotal");
			if(amountSubtotal==null) {
				amountSubtotal=new BigDecimal(0);
				dataMap.put("amount_subtotal", amountSubtotal);
			}
			BigDecimal qtyReturn=(BigDecimal)dataMap.get("qty_return");
			if(qtyReturn==null) {
				qtyReturn=new BigDecimal(0);
				dataMap.put("qty_return", qtyReturn);
			}
			BigDecimal amountSubtotalReturn=(BigDecimal)dataMap.get("amount_subtotal_return");
			if(amountSubtotalReturn==null) {
				amountSubtotalReturn=new BigDecimal(0);
				dataMap.put("amount_subtotal_return", amountSubtotalReturn);
			}
			BigDecimal qtyActual=qtyOrdered.subtract(qtyReturn);
			BigDecimal amountSubtotalActual=amountSubtotal.subtract(amountSubtotalReturn);
			
			dataMap.put("qty_actual", qtyActual);
			dataMap.put("amount_subtotal_actual", amountSubtotalActual);
		}
		return list;
	}
}
